package org.jeecg.modules.friends.user.controller;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.ControllerMethodCheck;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.modules.friends.dao.UserReportDAO;
import org.jeecg.modules.friends.enums.BaseInfoStatusEnum;
import org.jeecg.modules.friends.friendsinfo.service.IFriendsInfoService;
import org.jeecg.modules.friends.model.SystemConfigDO;
import org.jeecg.modules.friends.model.UserReportDO;
import org.jeecg.modules.friends.service.SystemConfigService;
import org.jeecg.modules.friends.service.VipUserService;
import org.jeecg.modules.friends.user.dto.HomePageUserDTO;
import org.jeecg.modules.friends.user.dto.ServiceConfigDTO;
import org.jeecg.modules.friends.user.dto.SysUserDTO;
import org.jeecg.modules.friends.user.dto.SysUserDetailDTO;
import org.jeecg.modules.friends.user.dto.UserReportDTO;
import org.jeecg.modules.friends.user.entity.User;
import org.jeecg.modules.friends.user.param.AuditBaseInfoReq;
import org.jeecg.modules.friends.user.param.UserParam;
import org.jeecg.modules.friends.user.param.FreezeOperateParam;
import org.jeecg.modules.friends.user.param.OperateAuthInfoParam;
import org.jeecg.modules.friends.user.param.UserAuthDetailParam;
import org.jeecg.modules.friends.user.param.UserPageParam;
import org.jeecg.modules.friends.user.param.UserReportHandleParam;
import org.jeecg.modules.friends.user.param.UserReportParam;
import org.jeecg.modules.friends.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.hutool.core.date.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * @author cold
 * @version 1.0
 * @data 2024/6/15 12:22
 */
@Slf4j
@Api(tags = "系统接口")
@RestController
@RequestMapping("/friends/sys/")
public class SystemOperateController {

    @Autowired
    private IUserService userService;
    @Autowired
    private SystemConfigService systemConfigService;

    @Autowired
    private VipUserService vipUserService;
    @Autowired
    private UserReportDAO userReportDAO;
    @Autowired
    private IFriendsInfoService friendsInfoService;

    @ApiOperation(value = "客服+会员链接", notes = "客服+会员链接")
    @RequestMapping(value = "/serviceConfig", method = {RequestMethod.POST})
    @ControllerMethodCheck
    public Result<ServiceConfigDTO> serviceConfig() {
        ServiceConfigDTO serviceConfigDTO = new ServiceConfigDTO();
        SystemConfigDO systemConfigDO = systemConfigService.getSystemConfig();
        serviceConfigDTO.setServiceConfigUrl(systemConfigDO.getServiceConfigUrl());
        serviceConfigDTO.setVipItemUrl(systemConfigDO.getVipItemUrl());
        return Result.OK(serviceConfigDTO);
    }

    @ApiOperation(value = "用户信息列表", notes = "用户信息列表")
    @RequestMapping(value = "/userPageList", method = {RequestMethod.POST})
    @ControllerMethodCheck
    public Result<Page<SysUserDTO>> userPageList(@RequestBody UserPageParam request) {

        Page<SysUserDTO> userDTOPage = userService.userPageList(request);
        return Result.OK(userDTOPage);
    }

    @ApiOperation(value = "用户信息列表Detail 给PC 用", notes = "用户信息列表Detail")
    @RequestMapping(value = "/userPageList/detail", method = {RequestMethod.POST})
    @ControllerMethodCheck
    public Result<Page<HomePageUserDTO>> userPageListDetail(@RequestBody UserPageParam request,
            @RequestHeader(value = "token", required = false) String token) {
        User user = null;
        if (StringUtils.isNotEmpty(token)) {
            user = getUser(token);
        }
        request.setAuthStatus(50);
        Page<HomePageUserDTO> page = userService.userPageListDetail(request, user);
        return Result.OK(page);
    }

    @ApiOperation(value = "用户信息详情", notes = "用户信息列表")
    @RequestMapping(value = "/userAuditDetail", method = {RequestMethod.POST})
    @ControllerMethodCheck
    public Result<SysUserDetailDTO> userAuditDetail(@RequestBody UserAuthDetailParam request) {
        SysUserDetailDTO sysUserDetailDTO = userService.userAuditDetail(request);
        return Result.OK(sysUserDetailDTO);
    }

    @ApiOperation(value = "用户冻结解冻", notes = "用户冻结解冻")
    @RequestMapping(value = "/freezeOperate", method = {RequestMethod.POST})
    @ControllerMethodCheck
    public Result<SysUserDetailDTO> freezeUser(@RequestBody FreezeOperateParam param) {
        userService.operateUser(param);
        return Result.OK("操作成功");
    }

    @ApiOperation(value = "用户注销", notes = "用户注销")
    @RequestMapping(value = "/cancelUser", method = {RequestMethod.POST})
    @ControllerMethodCheck
    public Result<?> cancelUser(@RequestBody UserParam param) {
        userService.cancelUser(param);
        return Result.OK("操作成功");
    }


    @ApiOperation(value = "用户认证信息操作", notes = "用户认证信息操作")
    @RequestMapping(value = "/operateAuthInfo", method = {RequestMethod.POST})
    @ControllerMethodCheck
    public Result<SysUserDetailDTO> operateAuthInfo(@RequestBody OperateAuthInfoParam param) {
        userService.operateAuthInfo(param);
        return Result.OK("操作成功");
    }

    @ApiOperation(value = "审核基本资料", notes = "审核基本信息")
    @RequestMapping(value = "/auditBaseInfo", method = {RequestMethod.POST})
    @ControllerMethodCheck
    public Result<?> auditBaseInfo(@RequestBody AuditBaseInfoReq param) {
        userService.updateBaseInfoStatus(param.getUserId(),
                BaseInfoStatusEnum.getByCode(param.getBaseInfoStatus()), param.getRemark());
        return Result.OK("操作成功");
    }

    @ApiOperation(value = "举报信息", notes = "举报信息")
    @RequestMapping(value = "/user/report/list", method = {RequestMethod.POST})
    @ControllerMethodCheck
    public Result<Page<UserReportDTO>> userReportList(@RequestBody UserReportParam param) {
        long l = userReportDAO.countUserReportPage(param.getPageNo(), param.getPageSize(), param.getId(), param);
        if (l == 0) {
            return Result.OK(new Page<>(param.getPageNo(), param.getPageSize(), 0));
        }
        List<UserReportDO> userReportDOS =
                userReportDAO.selectUserReportPage(param.getPageNo(), param.getPageSize(), param.getId(), param);
        List<UserReportDTO> userReportDTOList = ListUtils.emptyIfNull(userReportDOS).stream()
                .map((element) -> {
                    UserReportDTO userReportDTO = new UserReportDTO();
                    userReportDTO.setFromUserId(element.getFromUserId());
                    userReportDTO.setToUserId(element.getToUserId());
                    userReportDTO.setRemark(element.getRemark());
                    userReportDTO.setPics(element.getPics());
                    userReportDTO.setAddTimeStr(DateUtil.formatDateTime(element.getAddTime()));
                    userReportDTO.setUserReportId(element.getId());
                    userReportDTO.setHandleTagStr(element.getStatus() == 1 ? "未解决" : "已解决");
                    return userReportDTO;
                }).collect(Collectors.toList());
        Page<UserReportDTO> page =
                new Page<>(param.getPageNo(), param.getPageSize(), l);
        page.setRecords(userReportDTOList);
        return Result.OK(page);
    }

    @ApiOperation(value = "用户举报记录标记是否解决", notes = "用户举报记录标记是否解决")
    @RequestMapping(value = "/user/report/handle", method = {RequestMethod.POST})
    @ControllerMethodCheck
    public Result<?> userReportHandle(@RequestBody UserReportHandleParam param) {
        userReportDAO.update(param);
        return Result.OK();
    }

    @ApiOperation(value = "vip用户发券", notes = "vip用户发券")
    @RequestMapping(value = "/vipUserSendCoupon", method = {RequestMethod.POST})
    @ControllerMethodCheck
    public Result<?> vipUserSendCoupon(@RequestBody OperateAuthInfoParam param) {
        CompletableFuture.runAsync(() -> vipUserService.operateAuthInfo(param));
        return Result.OK("操作成功");
    }

    @ApiOperation(value = "重置每日刷", notes = "重置每日刷")
    @RequestMapping(value = "/reset/friend/record", method = {RequestMethod.POST})
    @ControllerMethodCheck
    public Result<?> resetFriendRecord(@RequestBody UserParam param) {
        friendsInfoService.resetFriendRecord(param.getUserIdsList());
        return Result.OK("操作成功");
    }

    private User getUser(String token) {
        String mobile = JwtUtil.getMobile(token);
        User user = userService.getUserByPhone(mobile);
        return user;
    }
}
